package com.liliudong.rommath.biz.sys.service.impl;


import com.liliudong.rommath.biz.sys.entity.SysRoleMenu;
import com.liliudong.rommath.biz.sys.mapper.SysRoleMenuMapper;
import com.liliudong.rommath.biz.sys.service.SysRoleMenuService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

import static com.liliudong.rommath.biz.sys.entity.table.SysRoleMenuTableDef.SYS_ROLE_MENU;

/**
 * 角色菜单关联表 服务层实现。
 *
 * @author liudong.li
 * @since 1.0.0
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

    @Override
    public void removeByRoleId(Long roleId) {
        remove(queryChain().where(SYS_ROLE_MENU.ROLE_ID.eq(roleId)));
    }

    @Override
    public void saveBatchByRoleId(Long sysRoleId, Collection<Long> menuIdList) {
        List<SysRoleMenu> sysRoleMenus = menuIdList.stream()
                .map(menuId -> SysRoleMenu.of(sysRoleId, menuId))
                .toList();
        saveBatch(sysRoleMenus);
    }

    @Override
    public List<Long> getParentMenuIdList(List<Long> menuIdList) {
        return queryChainIgnoreDataPermission()
                .select(SYS_ROLE_MENU.MENU_ID)
                .where(
                        SYS_ROLE_MENU.ID.in(menuIdList)
                )
                .listAs(Long.class);
    }
}